﻿using Microsoft.AspNetCore.Mvc;
using RBAC_Application.Dto.Input;
using RBAC_Application.Dto.Output;
using RBAC_Application.Service.RBAC;
using Standard;


namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IRbacService _rbacService;

        public UserController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        [HttpGet]
        public ApiResult<List<UserDto>> GetUsers([FromQuery] string keyword = null, bool? isActive = null)
        {
            try
            {
                var users = _rbacService.GetUsersByCondition(keyword, isActive);
                return new ApiResult<List<UserDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = users,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<UserDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        [HttpPost]
        public ApiResult<int> CreateUser([FromBody] CreateUserInput input)
        {
            try
            {
                _rbacService.CreateUser(input);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "创建成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        [HttpPut]
        public ApiResult<int> UpdateUser([FromBody] UpdateUserInput input)
        {
            try
            {
                _rbacService.UpdateUser(input);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "更新成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        [HttpDelete("{id}")]
        public ApiResult<int> DeleteUser(long id)
        {
            try
            {
                _rbacService.DeleteUser(id);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }


    }
}